package com.junzhiit.nengji.app.web.util.aliyun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.app.web.util.aliyun.bean.LogisticsList;
import com.junzhiit.nengji.app.web.util.aliyun.bean.LogisticsResultDate;
import com.junzhiit.nengji.app.web.util.aliyun.bean.ResultDate;

public class LogisticsUtil {

	private static Log logger = LogFactory.getLog(LogisticsUtil.class);
	
	private static String DEFAULT_TYPE_VALUE = "auto";
	
	private static String APPCODE = "0892caa735af4be3a1018f9ed735faf0";

	public static LogisticsResultDate expressQuery(String number,String type){
		if(StringUtils.isBlank(type))
			type = DEFAULT_TYPE_VALUE;
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("number", number);
	    querys.put("type", type);
	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(Config.URL_ALICLOUDAPI_HOST,Config.URL_LOGISTICS_EXPRESS_QUERY_PATH, RequestMethod.GET.toString(), headers, querys);
	    	String content = EntityUtils.toString(response.getEntity());
	    	logger.info("logistics express query data:"+content);
	    	Map<String, Object> map = JsonUtil.toMap(content);
	    	if(StringUtils.isNotBlank(map.get("result").toString())){
	    		return JsonUtil.toObject(content, LogisticsResultDate.class);
	    	}else{
	    		return null;
	    	}
	    	
	    	
	    } catch (Exception e) {
	    	logger.error(e.getMessage());
	    	return null;
	    }
	}
	
	public static List<LogisticsList> expressQueryToList(String number,String type){
		List<LogisticsList> list = new ArrayList<LogisticsList>();
		LogisticsResultDate logisticsResultDate = LogisticsUtil.expressQuery(number, type);
		if(logisticsResultDate!=null && logisticsResultDate.getResult()!=null){
			list =  logisticsResultDate.getResult().getList();
		}
		return list;
	}
	
	public static ResultDate expressType(){
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    try {
	    	HttpResponse response = HttpUtils.doGet(Config.URL_ALICLOUDAPI_HOST,Config.URL_LOGISTICS_EXPRESS_TYPE_PATH, RequestMethod.GET.toString(), headers, querys);
	    	String content = EntityUtils.toString(response.getEntity());
	    	logger.info("logistics express query data:"+content);
	    	return JsonUtil.toObject(content, ResultDate.class);
	    	
	    } catch (Exception e) {
	    	logger.error(e.getMessage());
	    	return null;
	    }
	}

	public static void main(String[] args) {
		expressQuery("278254828629","SFEXPRESS");
		expressType();
	}

}
